package com.efounder.form.upload;

import com.pansoft.library.CloudDiskBasicOperation;
import com.pansoft.library.upload.UploadThreadPoolUtils;
import com.pizidea.imagepicker.ui.LoadingDataUtilBlack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedHashMap;

import static com.efounder.form.util.CloudUtil.CLOUD_BASE_URL;
import static com.efounder.form.util.CloudUtil.CREATEUSER;
import static com.efounder.form.util.CloudUtil.DOWNURL;
import static com.efounder.form.util.CloudUtil.PARENTID;

/**
 * Created by apple on 2017/12/14.
 */

public class UploadTask extends UploadThreadPoolUtils.Task {
    private static CloudDiskBasicOperation operation = new CloudDiskBasicOperation();
    private final int mPosition;

    public String mPath;
    //    public CloudUtil.UpLoadListenerWithProgress upLoadListener;
    private final ImageUploadInfo ImageUploadInfo;

    public UploadTask(String id) {
//        this.upLoadListener = upLoadListener;
        ImageUploadInfo = UploadManager.getInstance().UPLOAD_INFO_HASHMAP.get(id);
        this.mPosition = ImageUploadInfo.getPosition();
        this.mPath = ImageUploadInfo.getLocalPath();
    }

    @Override
    protected void work() {
        String fileName = mPath.substring(mPath.lastIndexOf("/") + 1, mPath.length()).replace(".pic", "");
        operation.cloudDiskUploadWeChatFile(mPath, fileName, CREATEUSER, PARENTID, CLOUD_BASE_URL, new CloudDiskBasicOperation.UploadProgress() {
            @Override
            public void onUploadProgress(long bytesRead, long contentLength, boolean done) {
                int progress = (int) (bytesRead * 100 / contentLength);
//                if (upLoadListener != null) {
                if (progress < 100) {
                    setState(UploadState.UPLOADING);
                } else {
                    setState(UploadState.UPLOAD_COMPLETED);

                }
                UploadManager.getInstance().notifyObservesUpload(ImageUploadInfo.getLocalPath());
                ImageUploadInfo.setProgress(progress);
//                    upLoadListener.getHttpProgress(progress);
//                }
            }
        }, new CloudDiskBasicOperation.ReturnBack() {


            @Override
            public void onReturnBack(String result) throws Exception {
                String fileid = "";
                String resultString = null;
                try {
                    JSONObject resultObject = new JSONObject(result);
                    resultString = resultObject.getString("result");
                    if (resultObject.has("fileid")) {
                        fileid = resultObject.getString("fileid");
                    }
                } catch (JSONException e) {
                    setState(UploadState.UPLOAD_ERROR);
                    UploadManager.getInstance().notifyObservesUpload(ImageUploadInfo.getLocalPath());
                    UploadManager.getInstance().notifyUploadError();
                    UploadManager.getInstance().notifyMutiUploadError();
                    LoadingDataUtilBlack.dismiss();
                    e.printStackTrace();
                }

                if ("success".equals(resultString)) {
                    /**图片、视频、语音等资源路径*/
                    String resUrl = String.format(CLOUD_BASE_URL + DOWNURL, fileid);

//                    Log.i(TAG, resUrl);
//                    if (upLoadListener != null) {
//                    upLoadListener.getHttpUrl(true, resUrl);

                    ImageUploadInfo.setUrl(resUrl);
                    UploadManager.getInstance().notifyObservesUpload(ImageUploadInfo.getLocalPath());
                    if (null == UploadManager.getInstance().urlLinkedHashMap) {
                        UploadManager.getInstance().urlLinkedHashMap = new LinkedHashMap<>();

                    }
                    UploadManager.getInstance().urlLinkedHashMap.put(ImageUploadInfo.getPosition() + "", ImageUploadInfo.getUrl());

                    UploadManager.getInstance().forconference.put(ImageUploadInfo.getLocalPath(), ImageUploadInfo.getUrl());
                    if (UploadManager.getInstance().UPLOAD_INFO_HASHMAP.size() == UploadManager.getInstance().urlLinkedHashMap.size()) {
                        UploadManager.getInstance().notifyUploadSuccess(UploadManager.getInstance().urlLinkedHashMap);

                    }

                    if (UploadManager.getInstance().forconference.size() == UploadManager.getInstance().UPLOAD_INFO_HASHMAP.size()) {
                        UploadManager.getInstance().notifyUploadSuccess(UploadManager.getInstance().forconference);
                    }
//                    UploadManager.getInstance().notifyUploadSuccess(ImageUploadInfo.getLocalPath(),ImageUploadInfo.getPosition(),ImageUploadInfo.getUrl());
//                    }
                } else {
                    LoadingDataUtilBlack.dismiss();
                    setState(UploadState.UPLOAD_ERROR);
                    UploadManager.getInstance().notifyObservesUpload(ImageUploadInfo.getLocalPath());
                    UploadManager.getInstance().notifyUploadError();
                    UploadManager.getInstance().notifyMutiUploadError();



//                    upLoadListener.getHttpUrl(false, null);
                }
            }
        });
    }

    /**
     * 设置上传状态
     *
     * @param state
     */
    public void setState(int state) {
        ImageUploadInfo.setState(state);
        UploadManager.getInstance().notifyObservesUpload(ImageUploadInfo.getLocalPath());
        // 保存状态
//        FileEntity.saveEntityByAppId(downloadInfo,"cancel", mContext);
    }
}
